package ar.gps;

import java.util.Random;

import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.util.Log;
import ar.Location.Helper.CalculationProvider;
import ar.Location.Internet.NetworkStateReceiver;
import ar.Location.PlaceObject.PlaceCategoryManager;
import ar.camera.pojo.GeoLocation;
import ar.globals.Globals;

public class GPSListener implements LocationListener {

	private static final String TAG = "GPSListener";
	Random rand = new Random();
	private long lastUpdate = System.currentTimeMillis();
	private GeoLocation lastGeo;

	@Override
	public void onLocationChanged(Location location) {
		if (location != null) {
//			float latitude = 10.791859f;
//			float longitude = 106.662827f;
//			float latitude = 41.89001f;;
//			float longitude = 12.492828f;
			float latitude = (float) (location.getLatitude());
			float longitude = (float) (location.getLongitude());

			long currentTime = System.currentTimeMillis();
			if (currentTime - lastUpdate > Globals.PERIOD_OF_UPDATING) {
				if(lastGeo != null){
				//GeoLocation curr = Globals.currentGeoLocation;				
					float dist = CalculationProvider.distFrom(lastGeo.latitude,
							lastGeo.longitude, latitude, longitude);
					float velocity = dist / (currentTime - lastUpdate);
					Globals.velocity = (float) (Math.round(velocity * 10.0) / 10.0);
					// Log.v("velocity", String.valueOf(Math
					// .round(velocity * 100000) / 100000));
					lastUpdate = System.currentTimeMillis();
				}else{
					Globals.velocity = 0;
				}
				lastGeo = new GeoLocation(latitude, longitude, "");
			}

			Globals.currentGeoLocation = new GeoLocation(latitude, longitude,
					"Your location");
			if (Globals.locationIconView != null && Globals.compassView != null) {
				Globals.locationIconView.getmRenderer()
						.setmMyCurrentGeoLocation(Globals.currentGeoLocation);
				Globals.compassView.getmRenderer().setmMyCurrentGeoLocation(
						Globals.currentGeoLocation);
			}
			if (Globals.isSharingLocation) {

				if (Globals.mainClient1Thread != null) {
					Globals.mainClient1Thread
							.shareLatLngToServer(String.valueOf(latitude),
									String.valueOf(longitude));
				}
			}

			if (Globals.LAT != null && Globals.LNG != null
					&& Globals.LAT.length() > 0 && Globals.LNG.length() > 0) {
				float myLat = Float.parseFloat(Globals.LAT);
				float myLng = Float.parseFloat(Globals.LNG);

				float dist = CalculationProvider.distFrom(myLat, myLng,
						latitude, longitude);

				if (dist > 500) {
					Log.d(TAG, "load for dist change: " + dist);
					Globals.LAT = String.valueOf(latitude);
					Globals.LNG = String.valueOf(longitude);

					if (NetworkStateReceiver.isNetworkConnected) {
						if (!PlaceCategoryManager
								.isProcessingLoadListCategories()) {
//							PlaceCategoryManager.loadListCategories(latitude,
//									longitude, Globals.RADIUS);
						}

						if (!PlaceCategoryManager
								.isProcessingLoadListDataSources()) {
//							PlaceCategoryManager.loadListDataSources(latitude,
//									longitude, Globals.RADIUS);
						}
					}
				}
			} else {
				Log.d(TAG, "load for first time!");

				Globals.LAT = String.valueOf(latitude);
				Globals.LNG = String.valueOf(longitude);

				if (NetworkStateReceiver.isNetworkConnected) {
					if (!PlaceCategoryManager.isProcessingLoadListCategories()) {
//						PlaceCategoryManager.loadListCategories(latitude,
//								longitude, Globals.RADIUS);
					}

					if (!PlaceCategoryManager.isProcessingLoadListDataSources()) {
//						PlaceCategoryManager.loadListDataSources(latitude,
//								longitude, Globals.RADIUS);
					}
				}
			}
		}
	}

	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub
	}
}
